package com.cryptoneedle.center.source.dolphin.api;


import com.cryptoneedle.center.config.helper.ConfigPropertyHelper;
import com.cryptoneedle.center.source.dolphin.config.ClientResult;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class ProjectApiClient {

    private final RestTemplate restTemplate;

    public ProjectApiClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    private String base(String path) {
        String base = ConfigPropertyHelper.dsUrl();
        return path.startsWith("/") ? base + path : base + "/" + path;
    }

    public ClientResult<JsonNode> createProject(String projectName, String description) {
        String url = base("/projects");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("projectName", projectName);
        if (description != null) form.add("description", description);
        return restTemplate.postForObject(url, new HttpEntity<>(form, headers), ClientResult.class);
    }

    public ClientResult<JsonNode> updateProject(long code, String projectName, String description) {
        String url = base("/projects/" + code);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("projectName", projectName);
        if (description != null) form.add("description", description);
        ResponseEntity<ClientResult> resp = restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(form, headers), ClientResult.class);
        return resp.getBody();
    }

    public ClientResult<JsonNode> queryProjectByCode(long code) {
        String url = base("/projects/" + code);
        return restTemplate.getForObject(url, ClientResult.class);
    }

    public ClientResult<JsonNode> queryProjectListPaging(String searchVal, int pageSize, int pageNo) {
        StringBuilder sb = new StringBuilder("/projects?pageSize=").append(pageSize).append("&pageNo=").append(pageNo);
        if (searchVal != null) sb.append("&searchVal=").append(searchVal);
        return restTemplate.getForObject(base(sb.toString()), ClientResult.class);
    }

    public ClientResult<JsonNode> queryProjectWithAuthorizedLevelListPaging(Integer userId, String searchVal, Integer pageSize, Integer pageNo) {
        StringBuilder sb = new StringBuilder("/projects/project-with-authorized-level-list-paging?userId=").append(userId)
                .append("&pageSize=").append(pageSize).append("&pageNo=").append(pageNo);
        if (searchVal != null) sb.append("&searchVal=").append(searchVal);
        return restTemplate.getForObject(base(sb.toString()), ClientResult.class);
    }

    public ClientResult<JsonNode> deleteProject(long code) {
        String url = base("/projects/" + code);
        ResponseEntity<ClientResult> resp = restTemplate.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, ClientResult.class);
        return resp.getBody();
    }

    public ClientResult<JsonNode> queryUnauthorizedProject(Integer userId) {
        return restTemplate.getForObject(base("/projects/unauth-project?userId=" + userId), ClientResult.class);
    }

    public ClientResult<JsonNode> queryAuthorizedProject(Integer userId) {
        return restTemplate.getForObject(base("/projects/authed-project?userId=" + userId), ClientResult.class);
    }

    public ClientResult<JsonNode> queryProjectWithAuthorizedLevel(Integer userId) {
        return restTemplate.getForObject(base("/projects/project-with-authorized-level?userId=" + userId), ClientResult.class);
    }

    public ClientResult<JsonNode> queryProjectCreatedAndAuthorizedByUser() {
        return restTemplate.getForObject(base("/projects/created-and-authed"), ClientResult.class);
    }

    public ClientResult<JsonNode> queryAllProjectList() {
        return restTemplate.getForObject(base("/projects/list"), ClientResult.class);
    }

    public ClientResult<JsonNode> queryAllProjectListForDependent() {
        return restTemplate.getForObject(base("/projects/list-dependent"), ClientResult.class);
    }
}